package com.jiuli_travel.common;

import com.jiuli_travel.websocket.MessageWebSocketHandler;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/push")
public class PushController {

    private final MessageWebSocketHandler messageWebSocketHandler;

    public PushController(MessageWebSocketHandler messageWebSocketHandler) {
        this.messageWebSocketHandler = messageWebSocketHandler;
    }

    /**
     * 通过REST API接收消息并触发服务器向所有已连接的WebSocket客户端推送该消息。
     * @param message 要推送的字符串消息
     * @return ResponseEntity<Void>，表示操作成功
     */
    @PostMapping("/message")
    public ResponseEntity<Void> sendMessage(String message) {
        messageWebSocketHandler.broadcastMessage(message); // 调用MessageWebSocketHandler的broadcastMessage方法，向所有客户端推送消息
        return ResponseEntity.ok().build(); // 返回成功响应
    }
}